package com.leon.androidcoursedesign.ui.fragments.mainFragments;

import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.leon.androidcoursedesign.R;
import com.leon.androidcoursedesign.adapters.CourseAdapter;
import com.leon.androidcoursedesign.beans.Course;
import com.leon.androidcoursedesign.ui.dialogs.CourseDialog;
import com.leon.androidcoursedesign.ui.fragments.baseFragment.BaseFragment;
import com.leon.androidcoursedesign.ui.fragments.baseFragment.BindingUtils;
import com.leon.androidcoursedesign.utils.ValueUtils;
import com.leon.androidcoursedesign.utils.dataBaseUtils.CourseUtils;
import com.leon.androidcoursedesign.utils.dataBaseUtils.SQLiteHelperFactory;
import com.leon.androidcoursedesign.utils.dataBaseUtils.Tables;

import java.util.List;

public class CourseFragment extends BaseFragment implements OnClickListener, CourseAdapter.OnCourseItemListener, CourseDialog.OnRefreshListener {
    private static final String ADD_TITLE = "添加课程";
    private static final String EDIT_TITLE = "修改课程";

    private RecyclerView course_recyclerView;

    private TextView
            course_textView_name,
            course_textView_teacher,
            course_textView_location,
            course_textView_period,
            course_textView_credit;
    private ImageView course_imageView_add;

    private CourseUtils courseUtils;
    private CourseDialog courseDialog;
    private List<Course> courses;
    private CourseAdapter courseAdapter;

    public ObjectAnimator whirl;
    public ObjectAnimator reverse;

    @Override
    public int setLayout() {
        return R.layout.home_fragment_course;
    }

    @Override
    public void init() {
        String operationId = ValueUtils.getLoginId(context);

        SQLiteHelperFactory sqLiteHelperFactory = new SQLiteHelperFactory(context, Tables.Course);
        courseUtils = (CourseUtils) sqLiteHelperFactory.getInstance();

        courses = courseUtils.queryAllCourse(operationId);
    }

    @Override
    public void initView(BindingUtils bindingUtils) {
        course_textView_name = findView(R.id.course_textView_name);
        course_textView_teacher = findView(R.id.course_textView_teacher);
        course_textView_location = findView(R.id.course_textView_location);
        course_textView_period = findView(R.id.course_textView_period);
        course_textView_credit = findView(R.id.course_textView_credit);

        course_recyclerView = findView(R.id.course_recyclerView);

        course_imageView_add = findView(R.id.course_imageView_add);
        course_imageView_add.setOnClickListener(this);
    }

    @Override
    public void initValues() {
        //设置初始化数据
        if (courses != null && courses.size() != 0) {
            Course initCourseValue = courses.get(0);
            course_textView_name.setText(initCourseValue.courseName);
            course_textView_teacher.setText(initCourseValue.teacher);
            course_textView_location.setText(initCourseValue.location);
            course_textView_period.setText(initCourseValue.period);
            course_textView_credit.setText(initCourseValue.credit);
        }

        courseAdapter = new CourseAdapter(context, courses);
        courseAdapter.setOnCourseItemListener(this);
        course_recyclerView.setAdapter(courseAdapter);
        course_recyclerView.setLayoutManager(new LinearLayoutManager(context));

        // 初始化旋转动画
        whirl = ObjectAnimator.ofFloat(course_imageView_add, "rotation", 0.0f, 180.0f);
        whirl.setDuration(400);

        // 初始化翻转动画
        reverse = ObjectAnimator.ofFloat(course_imageView_add, "rotation", 0.0f, -180.0f);
        reverse.setDuration(400);
    }

    @Override
    public void onClick(View v) {
        setAnim(false);

        courseDialog = new CourseDialog(context, ADD_TITLE);
        courseDialog.setOnRefreshListener(this);
        courseDialog.show();
    }

    /**
     * 刷新该Fragment右边显示的course详情数据
     *
     * @param course    course对象
     */
    @Override
    public void refreshCourseDetail(Course course) {
        course_textView_name.setText(course.courseName);
        course_textView_teacher.setText(course.teacher);
        course_textView_location.setText(course.location);
        course_textView_period.setText(course.period);
        course_textView_credit.setText(course.credit);
    }

    /**
     * 修改指定course数据
     *
     * @param course    course
     */
    @Override
    public void editCourse(Course course) {
        // 创建修改course信息dialog
        courseDialog = new CourseDialog(context, EDIT_TITLE, course);
        courseDialog.setOnRefreshListener(this);
        courseDialog.show();
    }

    /**
     * 根据course name删除对应course
     *
     * @param itemId    itemId
     * @param position  该course在RecyclerView中索引位置
     */
    @Override
    public void deleteCourse(String itemId, int position) {
        // 从数据库中删除
        boolean deleteState = courseUtils.deleteCourse(itemId);
        Toast.makeText(context, deleteState ? "删除成功" : "删除失败", Toast.LENGTH_SHORT).show();

        // 从RecyclerView中删除
        courseAdapter.delete(position);
    }

    /**
     * 刷新RecyclerView中的course数据
     *
     * @param course    新添加的course
     */
    @Override
    public void onRefresh(Course course, boolean isEdit) {
        if (courseAdapter != null) {
            // 如果isEdit为false则调用append方法，否则调用replace方法
            if (!isEdit) {
                courseAdapter.append(course);
            } else {
                courseAdapter.replace(course);
            }
        }
    }

    /**
     * AddCourseDialog消失后的监听
     */
    @Override
    public void onDismiss() {
        // 设置动画为反转动画
        setAnim(true);
    }

    /**
     * 根据isRevers开始指定动画
     *
     * @param isRevers  是否开始反转动画
     */
    private void setAnim(boolean isRevers) {
        if (isRevers) {
            reverse.start();
        } else {
            whirl.start();
        }
    }

    @Override
    public void onDestroy() {
        courseUtils.close();
        super.onDestroy();
    }
}
